/**
 * Created by S on 2016/5/9.
 */
var commoditypictureProxy =require('../proxy/commoditypictureProxy');
var tools = require("../common/tool");
var async = require("async");

exports.insertcontent=function(req,res,callback){
    var query=url.parse(req.url,true).query;
    if(tools.isInteger(query.commodityid) && typeof (query.url)=="string")
    {
        //生成multiparty对象，并配置上传目标路径
        var form = new multiparty.Form({uploadDir: './public/files/'});
        //上传完成后处理
        form.parse(req, function(err, fields, files) {
            var filesTmp = JSON.stringify(files,null,2);
            if(err){
                console.log('parse error: ' + err);
                tools.answer(res,err,query.url);
            } else {
                var i=0;
                if(files.inputFile.length>0)
                {
                    var filename="";
                    filename=files.inputFile[i].path;
                    filename=filename.replace('public\\files\\','');
                    inserts(filename);
                    function inserts(pname)
                    {
                        if(pname.indexOf(".")>0)
                        {
                            commoditypictureProxy.insertcontent({name:pname,commodityid:query.id},function(err,data){
                                if(err)
                                {
                                    i++;
                                    if(i<files.inputFile.length)
                                    {
                                        filename=files.inputFile[i].path;
                                        filename=filename.replace('public\\files\\','');
                                        tools.deleteFile(pname);
                                        inserts(filename);
                                    }
                                    else
                                    {
                                        tools.answer(res,'上传失败',query.url);
                                    }
                                }
                                else
                                {
                                    i++;
                                    if(i<files.inputFile.length)
                                    {
                                        filename=files.inputFile[i].path;
                                        filename=filename.replace('public\\files\\','');
                                        inserts(filename);
                                    }
                                    else
                                    {
                                        tools.answer(res,'上传成功',query.url);
                                    }
                                }
                            });
                        }
                        else
                        {
                            i++;
                            if(i<files.inputFile.length)
                            {
                                filename=files.inputFile[i].path;
                                filename=filename.replace('public\\files\\','');
                                tools.deleteFile(pname);
                                inserts(filename);
                            }
                            else
                            {
                                tools.answer(res,'上传失败',query.url);
                            }
                        }

                    }
                }
                else
                {
                    tools.answer(res,'没有上传图片',query.url);
                }
            }
        });
    }
    else
    {
        tools.answer(res,'参数不完整','index.html');
    }
}

exports.findcontent = function (req, res,callback) {
    var param = req.body;
    var options={};
    async.waterfall([checkParam,search], function (error, result) {
        res.send(JSON.parse(tools.ResponseFormat(error, result)));
    });
    //检测参数
    function checkParam(cb) {
        if (param.id!=undefined) {
            options['id']=param.id;
        }
        if (param.commodityid!=undefined) {
            options['commodityid']=param.commodityid;
        }
        cb(null, null);
    }
    //查找
    function search(datas,cb)
    {
        commoditypictureProxy.findcontent(options,function (err, data) {
            if(err)
            {
                cb(err,null);
            }
            else
            {
                cb(null,data);
            }
        });
    }
};

exports.deletecontent=function(req,res,callback){
    var param=req.body;
    var option={};
    async.waterfall([checkParam,checkExist,deletes],function(error,result){
        res.send(JSON.parse(tools.ResponseFormat(error,result)));
    });
    function checkParam(cb)
    {
        if(!tools.isInteger(param.id))
        {
            cb('id不能为空',null);
        }
        else
        {
            option['id']=param.id;
            cb(null,null);
        }
    }
    //是否存在
    function checkExist(datas,cb)
    {
        commoditypictureProxy.findcontent({id:option.id},function (err, data) {
            if(err)
            {
                cb(err,null);
            }
            else if(!data || data.length<=0)
            {
                cb('项目不存在',null);
            }
            else
            {
                cb(null,null);
            }
        });
    }
    function deletes(datas,cb)
    {
        commoditypictureProxy.deletecontent(option,function(err,data){
            if(err)
            {
                cb(err,null);
            }
            else
            {
                cb(null,'删除成功');
            }
        });
    }
}